package com.robusta.cityuate.dialogs;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnShowListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

import com.robusta.cityuate.R;

public class PasswordConfirmationDialog extends DialogFragment implements
		OnEditorActionListener,TextWatcher {

	AlertDialog.Builder alert;

	public interface PasswordEnteredCallBack {
		public void onPasswordConfirmed(String password);
	}

	public static PasswordConfirmationDialog getInstant() {
		PasswordConfirmationDialog f = new PasswordConfirmationDialog();
		return f;
	}

	private PasswordEnteredCallBack mListener;
	private View view;
	private EditText passField;
	private AlertDialog dialog;

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		LayoutInflater inflater = getActivity().getLayoutInflater();
		view = inflater.inflate(R.layout.confirm_password_view, null);
		passField = (EditText) view.findViewById(R.id.password);
		passField.addTextChangedListener(this);
		builder.setView(view)
				// Add action buttons
				.setPositiveButton(android.R.string.ok,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
								dismiss();
								mListener.onPasswordConfirmed(passField
										.getText().toString());
							}
						})
				.setNegativeButton(R.string.cancel,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dismiss();
							}
						});

		builder.setTitle(R.string.confirm_update);
		builder.setMessage(R.string.enter_current_password);
		dialog= builder.create();
		dialog.setOnShowListener(new OnShowListener() {
			
			@Override
			public void onShow(DialogInterface dialog2) {
				dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
				
			}
		});

		return dialog;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mListener = (PasswordEnteredCallBack) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement OnPasswordEnteredListener");
		}
	}

	@Override
	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		dismiss();
		mListener.onPasswordConfirmed(passField.getText().toString());
		return true;
	}

	@Override
	public void afterTextChanged(Editable s) {
		
		
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
		
		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		if(s.length()>=1)
			dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(true);
		else
			dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
		
		
	}

}
